// copyright jyz.2024
#include "AuraCharacter.h"

#include "AbilitySystemComponent.h"
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "AuraRPGDemo/Player/AuraPlayerState.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Player/AuraPlayerController.h"
#include "UI/HUD/AuraHUD.h"

AAuraCharacter::AAuraCharacter()
{
	//角色在移动时自动调整其面向角度，使其始终朝向移动方向
	GetCharacterMovement()->bOrientRotationToMovement=true;
	//设定玩家角色旋转的速度。具体来说，它设置了一个旋转速率值 FRotator，其中的 400.f 指的是角度/秒，表示角色每秒可以旋转400度
	GetCharacterMovement()->RotationRate = FRotator(0.f,400.f,0.f);
	//约束角色只在一个平面上移动，例如在2.5D游戏中，角色只在一个二维平面上运动，无法上下飞跃或跳跃。
	GetCharacterMovement()->bConstrainToPlane = true;
	//如果你的角色以某种方式开始时没有位于限制平面上，这行代码将在开始时自动将角色投影（或"对齐"）到限制平面上
	GetCharacterMovement()->bSnapToPlaneAtStart =true;

	bUseControllerRotationPitch=false;
	bUseControllerRotationRoll=false;
	bUseControllerRotationYaw=false;
	
	MySpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringComp"));
	MySpringArmComponent->AttachToComponent(GetMesh(),FAttachmentTransformRules::KeepRelativeTransform);
	MySpringArmComponent->SetRelativeLocation(FVector(0,0,90));
	MySpringArmComponent->SetRelativeRotation(FRotator(-45,0,0));
	MySpringArmComponent->bInheritPitch = false;
	MySpringArmComponent->bInheritRoll = false;
	MySpringArmComponent->bInheritYaw = false;
	MySpringArmComponent->bUsePawnControlRotation = false;
	MySpringArmComponent->TargetArmLength = 800;
	MySpringArmComponent->bDoCollisionTest = true;
	MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));
	MyCamera->AttachToComponent(MySpringArmComponent,FAttachmentTransformRules::KeepRelativeTransform);
}

void AAuraCharacter::PossessedBy(AController* NewController)
{
	// Init ability actor info for thi Server;
	Super::PossessedBy(NewController);
	InitAbilityActorInfo();
	//添加能力
	AddGameAbilities();
}

void AAuraCharacter::OnRep_PlayerState()
{
	// Init ability actor info for thi Client;
	Super::OnRep_PlayerState();
	InitAbilityActorInfo();
}

int32 AAuraCharacter::GetCharacterLevel() const
{
	const AAuraPlayerState* _PlayerState = Cast<AAuraPlayerState>(GetPlayerState());
	check(_PlayerState);
	return _PlayerState->GetCharacterLevel();
}

void AAuraCharacter::InitAbilityActorInfo()
{
	AAuraPlayerState* _PlayerState = Cast<AAuraPlayerState>(GetPlayerState());
	check(_PlayerState);
	AbilitySystemComponent = _PlayerState->GetAbilitySystemComponent();
	AbilitySystemComponent->InitAbilityActorInfo(_PlayerState,this);
	Cast<UAuraAbilitySystemComponent>(AbilitySystemComponent)->AbilityActorInfoSet();
	AttributeSet = _PlayerState->GetAttributeSet();
	if(AAuraPlayerController*AuraPlayerController = Cast<AAuraPlayerController>(GetController()))
	{
		if(AAuraHUD*AuraHUD = Cast<AAuraHUD>(AuraPlayerController->GetHUD()))
		{
			AuraHUD->InitOverlay(AuraPlayerController,_PlayerState,AbilitySystemComponent,AttributeSet);
		}
	}
	Super::InitAbilityActorInfo();
}
 